package com.controller.admin;

import com.controller.BaseController;
import com.entity.GoodsCatalog;
import com.github.pagehelper.PageInfo;
import com.service.imple.admin.GoodsCatalogServiceImple;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品分类
 */

@Controller
@RequestMapping("admin/GoodsProduct")
public class GoodsProductController extends BaseController<GoodsCatalogServiceImple> {

    @RequestMapping("toGoodsProduct")
    public String toGoodsProduct(){
        return "center/admin/product/goodsProduct";
    }

    @RequestMapping("GoodsProduct")
    public String goodsManage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum, ModelMap modelMap){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",pageNum);

        PageInfo<GoodsCatalog> pageInfo = BaseService.selectByPage(map);
        modelMap.put("pageInfo",pageInfo);
        return "center/admin/product/goodsPage";
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        BaseService.delete(id);
        return "center/admin/product/goodsProduct";
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "center/admin/product/addGoodsProduct";
    }

    @RequestMapping("Add")
    @ResponseBody
    public Map<String,Object> Add(GoodsCatalog goodsCatalog){
       int count = BaseService.insert(goodsCatalog);
        Map<String,Object> map = new HashMap<>();
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    @RequestMapping("toUpdate")
    public String  toUpdate(Integer id,ModelMap modelMap){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        List<GoodsCatalog> list = BaseService.select(map);
        modelMap.put("list",list);
        return "center/admin/product/index";
    }

    @RequestMapping("Update")
    @ResponseBody
    public Map<String,Object> Update(Integer id, String name, String remark ){
        Map<String,Object> map = new HashMap<>();
        GoodsCatalog goodsCatalog = new GoodsCatalog();
        goodsCatalog.setId(id);
        goodsCatalog.setName(name);
        goodsCatalog.setRemark(remark);
        int count = BaseService.update(goodsCatalog);
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

}
